using System;
using System.Collections.Generic;
using System.Text;
using SharpMud.LPC;

namespace SharpMud.Net.Intermud3
{
    /// <summary>
    /// Encapsulates base functionality (and centralized recognition) for all packet types.
    /// </summary>
    public class GenericPacket : PacketBase
    {
        protected SharpMud.LPC.Array _data;

        #region Constructors
        protected GenericPacket()
        {
            _data = new LPC.Array();
        }
        protected  GenericPacket(LPC.Object[] data)
            :this(new LPC.Array(data))
        {
        }
        protected GenericPacket(LPC.Array data)
        {
            if (data.Count < 6)
                throw new InvalidPacketException("There is not enough data in the given Array to create a GenericPacket.");
            _data = data;
        }
        #endregion

        #region Public Properties

        #endregion

        #region Conversion Operators
        public static explicit operator SharpMud.Net.MudModePacket(GenericPacket val)
        {
            if (val == null)
                throw new ArgumentNullException("val", "Attempt to convert a null Intermud3 packet to a MudModePacket.");
            return new MudModePacket(System.Text.Encoding.ASCII.GetBytes(val._data.ToParsableString()));
        }

        public static explicit operator SharpMud.LPC.Array(GenericPacket val)
        {
            if (val == null)
                throw new ArgumentNullException("val", "Attempt to convert a null Intermud3 packet to an LPC Array.");
            return (SharpMud.LPC.Array)((ICloneable)val._data).Clone();
        }
        public static explicit operator GenericPacket(SharpMud.LPC.Array val)
        {
            if (val == null)
                throw new ArgumentNullException("val", "Attempt to convert a null LPC Array to an Intermud3 packet.");
            if (val.Count <= 0)
                throw new ArgumentException("Attempt to convert an empty LPC Array to an Intermud3 packet.","val");
            return (GenericPacket)GenericPacket.CreatePacket(val);
        }
        #endregion

        public static PacketBase CreatePacket(SharpMud.LPC.Array lpcArray)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}